package com.rz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.admin.entity.AdminCollectMenu;
import com.rz.admin.entity.Res;
import com.rz.admin.mapper.AdminCollectMenuMapper;
import com.rz.admin.service.IAdminCollectMenuService;
import com.rz.admin.service.IResService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class AdminCollectMenuService extends ServiceImpl<AdminCollectMenuMapper, AdminCollectMenu> implements IAdminCollectMenuService {

    @Resource private IResService resService;

    @Override
    public List<Res> getByAdminId(Integer adminId) {
        QueryWrapper<AdminCollectMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id",adminId);
        List<AdminCollectMenu> adminCollectMenus = list(queryWrapper);
        List<Integer> resIds = new ArrayList<>();
        Optional.ofNullable(adminCollectMenus).orElse(new ArrayList<>()).forEach(adminCollectMenu -> resIds.add(adminCollectMenu.getMenuId()));
        return resIds.size() ==0 ? new ArrayList<>() : new ArrayList<>(resService.listByIds(resIds));
    }

    @Override
    public boolean onSave(AdminCollectMenu adminCollectMenu) {
        QueryWrapper<AdminCollectMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id",adminCollectMenu.getAdminId());
        queryWrapper.eq("menu_id",adminCollectMenu.getMenuId());
        AdminCollectMenu menu = getOne(queryWrapper);
        if(menu!=null){
            return removeById(menu.getId());
        }else {
            return save(adminCollectMenu);
        }
    }
}
